﻿//********************************************************************************//
//创建日期：<创建日期，2016-05-30>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：系统
//********************************************************************************//

using System.Collections.Generic;
using Newtonsoft.Json;
using Entity;
using BLL;
using YXYLCommon;
using System.Web;
using System.Web.SessionState;
using Platform.Permission;
using System.Text;
using System;
using System.IO;
using BLL.Access;

namespace Web.ashx.XT
{
    public class XT : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int? rsno = -1;
            string rsmsg = "";
            if (context.Request["op"] == "yhxx")
            {
                string name = context.Request["name"];
                int bm = int.Parse(context.Request["bm"]);
                int active = int.Parse(context.Request["active"]);
                List<XTUSER> list = new DALXTUSER().Get(name,bm,active);
                context.Response.Write(JsonConvert.SerializeObject(list));
            }
            else if (context.Request["op"] == "yhxx_detail")
            {
                int id = int.Parse(context.Request["id"]);
                context.Response.Write(JsonConvert.SerializeObject(new DALXTUSER().GetInfo(id)));
            }
            else if (context.Request["op"] == "yhxx_save")
            {
                string mode = context.Request["mode"];
                XTUSER obj =new XTUSER();
                obj.Operate = (mode == "edit") ? eModelOperate.Update : eModelOperate.Add;
                obj.ACCOUNT=context.Request["iAccount"];
                obj.USERID = int.Parse(context.Request["iAccount"]);
                obj.USERNAME = context.Request["iUserName"];
                obj.ISACTIVE = int.Parse(context.Request["iActive"]);
                obj.DEPARTMENT = int.Parse(context.Request["iBM"]);
                obj.TELEPHONE = context.Request["iTelephone"];
                obj.MOBILEPHONE = context.Request["iMobilephone"];
                obj.EMAIL = context.Request["iEmail"];
                obj.IDCARD = context.Request["iID_card"];
                obj.BIRTHDAY = context.Request["iBirthday"];
                obj.GENDER = int.Parse(context.Request["iGender"]);
                obj.ADDRESS = context.Request["iAddress"];
                obj.POSTCODE = context.Request["iPostcode"];
                obj.USERDESC = context.Request["iDesc"];
                DbCmdResult dbrs = new DALXTUSER().Save(obj);
                JsonRs rs = new JsonRs(dbrs.IsSuccess, dbrs.ReturnMsg, null);
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }
            else if (context.Request["op"] == "logout")
            {
                string rsno1 = "-1";
                string rsmsg1 = "";
                new BLL.Access.SpHelper().P_LOGIN(LoginHelper.UserID, 2, out rsno1, out rsmsg1);
            }
            else if (context.Request["op"] == "role")
            {
                
                var dtRoleInfo = PermissionSetting.GetRoleInfo();
                var json = JsonConvert.SerializeObject(dtRoleInfo);
                context.Response.Write(json);
            }
            else if (context.Request["op"] == "RoleList")//获取报表
            {
                DALBaoBiaoDY dy = new DALBaoBiaoDY();
                string RoleID = context.Request["RoleID"];
                int authorityType=context.Request["authorityType"].GetNoNullInt32();
                var s = dy.GetRoleByRoleID(RoleID, authorityType, BusinessTool.WType, 1);
                context.Response.Write(JsonConvert.SerializeObject(s));
            }
            else if (context.Request["op"] == "SaveReportRole")
            {
                int RoleID = context.Request["RoleID"].GetNoNullInt32();
                int authorityType = context.Request["authorityType"].GetNoNullInt32();
                var arrReportID = context.Request["arrReportID"];
                DALBaoBiaoDY dy = new DALBaoBiaoDY();
                var result = dy.SaveReportRole(RoleID, authorityType, arrReportID, BusinessTool.WType);
                var json = JsonConvert.SerializeObject(result);
                context.Response.Write(json);
            }
            else if (context.Request["op"] == "loadReportRole")
            {
                int RoleID = context.Request["RoleID"].GetNoNullInt32();
                int authorityType = context.Request["authorityType"].GetNoNullInt32();
                DALBaoBiaoDY dy = new DALBaoBiaoDY();
                var s= dy.loadReportRole(RoleID, authorityType);
                var r = JsonConvert.SerializeObject(s);
                context.Response.Write(r);
            }
            //else if (context.Request["op"] == "DY")
            //{
            //    //获取打印状态,账单编号,发票号码,用户号,用户名,开户户名
            //    string dyzt = context.Request["dyzt"];
            //    string zdbh = context.Request["zdbh"];
            //    string fphm = context.Request["fphm"];
            //    string yhh = context.Request["yhh"];
            //    string yhm = context.Request["yhm"];
            //    string khhm = context.Request["khhm"];
            //    //成功:1 打坏:2
            //    new SpHelper().P_QSSF_JJZD_DY(dyzt == "success" ? 1 : 2, BusinessTool.ZHANDIANBH() == "00" ? "51" : BusinessTool.ZHANDIANBH(), zdbh, fphm, yhh, yhm, khhm, DateTime.Now, LoginInfo.CurrentUser.UserID.ToString(), out rsno, out rsmsg);
            //    JsonRs rs = new JsonRs(rsno == 1, rsmsg, null);
            //    context.Response.Write(JsonConvert.SerializeObject(rs));
            //}
            else if (context.Request["op"] == "SCDZQM")
            {
                string userid = context.Request["UserID"];
                //string username = context.Request["username"]; 
                string UploadDirectory = "/UpLoad/USER/DZQM";
                int a = context.Request.Files.Count;
                HttpPostedFile file = context.Request.Files[0];
                string fileName = Guid.NewGuid() + file.FileName;
                //string filename = file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);
                string filePath = System.Web.HttpContext.Current.Server.MapPath(UploadDirectory);
                //string filefullpath=filePath + "\\" + fileName;
                file.SaveAs(filePath + "\\" + fileName);
                //存入数据库，根据用户id，存储对应的电子签名路径
                SpHelper sp = new SpHelper();
                JsonRs rs = new JsonRs();
                string msg = "";
                sp.P_XT_SCDZQM(userid, UploadDirectory + "/" + fileName, out msg);
                if (msg != "")
                {
                    rs.State = true;
                    rs.Msg = fileName;
                }
                else
                {
                    rs.State = false;
                }
                context.Response.Write(JsonConvert.SerializeObject(rs));
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}